//
//  CompleteViewController.m
//  inquiry
//
//  Created by 高宇 on 2020/6/1.
//  Copyright © 2020 高宇. All rights reserved.
//
#define SCREEN_WIDTH                 ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT                ([[UIScreen mainScreen] bounds].size.height)
#define WEAKSELF   __typeof(&*self) __weak weakSelf = self;

#import "CompleteViewController.h"
#import "PersonalView.h"
#import "SheelViewController.h"
#import "CompleteViewController2.h"
#import "GXToast.h"
#import "ULNetHelper.h"
#import "Interface.h"

@interface CompleteViewController ()<PersonalDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UITextField *nameTf;
@property (nonatomic, strong) UILabel *sexLabel;
@property (nonatomic, strong) UIButton *boyButton;
@property (nonatomic, strong) UIButton *girlButton;
@property (nonatomic, strong) PersonalView *numView;
@property (nonatomic, strong) PersonalView *schoolView;
@property (nonatomic, strong) PersonalView *gradeView;
@property (nonatomic, strong) PersonalView *classView;

@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, assign) BOOL isBoy;

//存放接口数据
@property (nonatomic, strong) NSArray *schoolArr;
@property (nonatomic, strong) NSArray *gradeArr;
@property (nonatomic, strong) NSArray *classArr;

@end

@implementation CompleteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //创建UI
    [self createUI];
    
    [self getData];
    
    self.isBoy = YES;
}

- (void)createUI{
    
    int gao = 40;
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            gao = 64;
        }
    }
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_nextButton.frame));
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, gao, SCREEN_WIDTH-60, 33)];
    label.text = @"请完善个人资料";
    label.font = [UIFont systemFontOfSize:24];
    label.textColor = [UIColor blackColor];
    [self.scrollView addSubview:label];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(33, gao+42, SCREEN_WIDTH-60, 22)];
    label2.text = @"温馨提示，请在家长的帮助下完成～";
    label2.font = [UIFont systemFontOfSize:16];
    label2.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    [self.scrollView addSubview:label2];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.layer.cornerRadius = 10;
    self.button.frame = CGRectMake(30, gao+90, SCREEN_WIDTH-60, 48);
    self.button.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
    [self.scrollView addSubview:self.button];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 13, 64, 22)];
    self.label.font = [UIFont systemFontOfSize:16];
    self.label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
    self.label.text = @"姓名";
    [self.button addSubview:self.label];
    
    self.nameTf = [[UITextField alloc] initWithFrame:CGRectMake(98, 13, self.button.frame.size.width-121, 22)];
    self.nameTf.font = [UIFont systemFontOfSize:16];
    self.nameTf.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
    self.nameTf.text = @"";
    self.nameTf.placeholder = @"请输入姓名";
    self.nameTf.delegate = self;
    [self.button addSubview:self.nameTf];
    
    self.sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(49, CGRectGetMaxY(self.button.frame)+33, 64, 22)];
    self.sexLabel.font = [UIFont systemFontOfSize:16];
    self.sexLabel.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
    self.sexLabel.text = @"性别";
    [self.scrollView addSubview:self.sexLabel];
    
    self.boyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.boyButton.frame =CGRectMake(CGRectGetMaxX(self.sexLabel.frame), self.sexLabel.frame.origin.y, 85, 22);
    [self.boyButton setImage:[UIImage imageNamed:@"checkbox／selected+normal"] forState:0];
    [self.boyButton setTitleColor:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] forState:0];
    [self.boyButton setTitle:@" 小帅哥" forState:0];
    [self.scrollView addSubview:self.boyButton];
    [self.boyButton addTarget:self action:@selector(sexClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.girlButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.girlButton.frame =CGRectMake(CGRectGetMaxX(self.boyButton.frame)+15, self.sexLabel.frame.origin.y, 85, 22);
    [self.girlButton setImage:[UIImage imageNamed:@"checkbox／unselected+normal"] forState:0];
    [self.girlButton setTitleColor:[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0] forState:0];
    [self.girlButton setTitle:@" 小美女" forState:0];
    [self.girlButton addTarget:self action:@selector(sexClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.girlButton setSelected:NO];
    [self.scrollView addSubview:self.girlButton];
    
    self.numView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.sexLabel.frame)+33, SCREEN_WIDTH-60, 48)];
    self.numView.label.text = @"年龄";
    self.numView.delegate = self;
    [self.scrollView addSubview:self.numView];
    
    self.schoolView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.numView.frame)+20, SCREEN_WIDTH-60, 48)];
    self.schoolView.label.text = @"学校";
    self.schoolView.delegate = self;
    [self.scrollView addSubview:self.schoolView];
    
    self.gradeView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.schoolView.frame)+20, SCREEN_WIDTH-60, 48)];
    self.gradeView.label.text = @"年级";
    self.gradeView.delegate = self;
    [self.scrollView addSubview:self.gradeView];
    
    self.classView = [[PersonalView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.gradeView.frame)+20, SCREEN_WIDTH-60, 48)];
    self.classView.label.text = @"班级";
    self.classView.delegate = self;
    [self.scrollView addSubview:self.classView];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextButton.frame = CGRectMake(self.view.frame.size.width/2-75, CGRectGetMaxY(self.classView.frame)+40, 150, 40);
    [self.nextButton setTitle:@"下一步" forState:0];
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:0];
    self.nextButton.backgroundColor = [UIColor colorWithRed:110/255.0 green:132/255.0 blue:238/255.0 alpha:1];
    self.nextButton.layer.cornerRadius = 10;
    [self.nextButton addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:self.nextButton];
    
    //最后设置contentSize
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGRectGetMaxY(_nextButton.frame)+40);
}

- (void)nextClick:(UIButton *)button{
    if ([self.nameTf.text isEqualToString:@""]) {
        [GXToast showText:@"请填写用户名"];
        return;
    }else if ([self.schoolView.mainlabel.text isEqualToString:@"请选择"]) {
        [GXToast showText:@"请选择学校"];
        return;
    }else if ([self.gradeView.mainlabel.text isEqualToString:@"请选择"]) {
        [GXToast showText:@"请选择年级"];
        return;
    }else if ([self.classView.mainlabel.text isEqualToString:@"请选择"]) {
        [GXToast showText:@"请选择班级"];
        return;
    }
    
    NSString *nameStr = self.nameTf.text;
    NSString *sexStr;
    if (self.isBoy) {
        sexStr = @"man";
    }else{
        sexStr = @"woman";
    }
    NSString *numStr = self.numView.idStr;
    NSString *schoolStr = self.schoolView.idStr;
    NSString *gradeStr = self.gradeView.idStr;
    NSString *classStr = self.classView.idStr;
    
    CompleteViewController2 *vc = [[CompleteViewController2 alloc] init];
    vc.nameStr = nameStr;
    vc.sexStr = sexStr;
    vc.numStr = numStr;
    vc.schoolStr = schoolStr;
    vc.gradeStr = gradeStr;
    vc.classStr = classStr;
    vc.identityId = self.identityId;
    vc.modalPresentationStyle =  UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}


- (void)sexClick:(UIButton *)button{
    
    if (button == _boyButton) {
        if (button.selected) {
            [self.boyButton setImage:[UIImage imageNamed:@"checkbox／unselected+normal"] forState:0];
            [self.girlButton setImage:[UIImage imageNamed:@"checkbox／selected+normal"] forState:0];
            self.isBoy = NO;
        }else{
            [self.boyButton setImage:[UIImage imageNamed:@"checkbox／selected+normal"] forState:0];
            [self.girlButton setImage:[UIImage imageNamed:@"checkbox／unselected+normal"] forState:0];
            self.isBoy = YES;
        }
    }
    else if (button == _girlButton) {
        if (button.selected) {
            [self.boyButton setImage:[UIImage imageNamed:@"checkbox／selected+normal"] forState:0];
            [self.girlButton setImage:[UIImage imageNamed:@"checkbox／unselected+normal"] forState:0];
            self.isBoy = YES;
        }else{
            [self.boyButton setImage:[UIImage imageNamed:@"checkbox／unselected+normal"] forState:0];
            [self.girlButton setImage:[UIImage imageNamed:@"checkbox／selected+normal"] forState:0];
            self.isBoy = NO;
        }
    }
    button.selected = !button.selected;
}


#pragma mark personal代理
- (void)personalClick:(UIView *)personalView{
    NSArray *arr;
    if (personalView == _schoolView) {
        arr = _schoolArr;
    }else if(personalView == _gradeView) {
        arr = _gradeArr;
    }else if(personalView == _classView) {
        arr = _classArr;
    }else if(personalView == _numView) {
        NSMutableArray *array = [NSMutableArray array];
        
        for(int i = 0; i < 26; i++)
        {
            NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
            [tempDic setValue:[NSString stringWithFormat:@"%d",i]  forKey:@"id"];
            [tempDic setValue:[NSString stringWithFormat:@"%d",i]  forKey:@"name"];
            [array addObject:tempDic];
        }
        arr = array;
    }
    
    SheelViewController *vc = [[SheelViewController alloc] init];
    vc.arr = arr;
    [[UIApplication sharedApplication].keyWindow addSubview:vc.view];
    [self addChildViewController:vc];
    
    __typeof(&*vc) __weak weakVC = vc;
    vc.block = ^{
        [weakVC.view removeFromSuperview];
        [weakVC removeFromParentViewController];
    };
    
    PersonalView *perView = (PersonalView *)personalView;
    vc.tureblock = ^(NSDictionary *dic) {
        perView.mainlabel.text = dic[@"name"];
        perView.idStr = dic[@"id"];
        [weakVC.view removeFromSuperview];
        [weakVC removeFromParentViewController];
    };
}

#pragma mark 文本框代理
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}

- (void)getData{
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务1
        dispatch_group_enter(group);   //标志着一个任务追加到 group，执行一次，相当于 group 中未执行完毕任务数+1
        //网络请求无论成功或失败都需要调用dispatch_group_leave(group)方法任务数减一
        [ULNetHelper GET:schoolListPath parameters:nil success:^(id responseObject) {
            NSDictionary *dic = responseObject;
            long code = [dic[@"code"] longValue];
            if (code == 200) {
                NSMutableArray *array = [NSMutableArray array];
                NSArray *arr = dic[@"data"];
                for (NSDictionary *dic in arr) {
                    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
                    [tempDic setValue:[NSString stringWithFormat:@"%@",dic[@"schoolId"]]  forKey:@"id"];
                    [tempDic setValue:dic[@"schoolName"] forKey:@"name"];
                    [array addObject:tempDic];
                }
                self.schoolArr = array;
            }else{
                NSLog(@"学校列表请求失败");
            }
            dispatch_group_leave(group);  //标志着一个任务离开了 group，执行一次，相当于 group 中未执行完毕任务
        }];
    });

    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务2
        dispatch_group_enter(group);
        [ULNetHelper GET:gradesListPath parameters:nil success:^(id responseObject) {
            NSDictionary *dic = responseObject;
            long code = [dic[@"code"] longValue];
            if (code == 200) {
                NSMutableArray *array = [NSMutableArray array];
                NSArray *arr = dic[@"data"];
                for (NSDictionary *dic in arr) {
                    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
                    [tempDic setValue:[NSString stringWithFormat:@"%@",dic[@"gradeId"]]  forKey:@"id"];
                    [tempDic setValue:dic[@"gradeName"] forKey:@"name"];
                    [array addObject:tempDic];
                }
                self.gradeArr = array;
            }else{
                NSLog(@"年级列表请求失败");
            }
            dispatch_group_leave(group);
        }];
    });
    
    dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 追加任务3
        dispatch_group_enter(group);
        [ULNetHelper GET:classesListPath parameters:nil success:^(id responseObject) {
            NSDictionary *dic = responseObject;
            long code = [dic[@"code"] longValue];
            if (code == 200) {
                NSMutableArray *array = [NSMutableArray array];
                NSArray *arr = dic[@"data"];
                for (NSDictionary *dic in arr) {
                    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
                    [tempDic setValue:[NSString stringWithFormat:@"%@",dic[@"classesId"]]  forKey:@"id"];
                    [tempDic setValue:dic[@"classesName"] forKey:@"name"];
                    [array addObject:tempDic];
                }
                self.classArr = array;
            }else{
                NSLog(@"班级列表请求失败");
            }
            dispatch_group_leave(group);
        }];
    });
}

@end
